home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / OR3B68 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.7 KB  |  196 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import java.awt.Rectangle;
  4. import java.awt.Shape;
  5.  
  6. public abstract class CompositeView extends View {
  7.    private static View[] ONE = new View[1];
  8.    private static View[] ZERO = new View[0];
  9.    private View[] children = new View[1];
  10.    private int nchildren = 0;
  11.    private short left;
  12.    private short right;
  13.    private short top;
  14.    private short bottom;
  15.  
  16.    public CompositeView(Element elem) {
  17.       super(elem);
  18.    }
  19.  
  20.    public void append(View v) {
  21.       ONE[0] = v;
  22.       this.replace(this.nchildren, 0, ONE);
  23.    }
  24.  
  25.    protected abstract void childAllocation(int var1, Rectangle var2);
  26.  
  27.    protected final short getBottomInset() {
  28.       return this.bottom;
  29.    }
  30.  
  31.    public Shape getChildAllocation(int index, Shape a) {
  32.       Rectangle alloc = a.getBounds();
  33.       this.childAllocation(index, alloc);
  34.       return alloc;
  35.    }
  36.  
  37.    protected Rectangle getInsideAllocation(Shape a) {
  38.       if (a != null) {
  39.          Rectangle alloc = new Rectangle(a.getBounds());
  40.          alloc.x += this.left;
  41.          alloc.y += this.top;
  42.          alloc.width -= this.left + this.right;
  43.          alloc.height -= this.top + this.bottom;
  44.          return alloc;
  45.       } else {
  46.          return null;
  47.       }
  48.    }
  49.  
  50.    protected final short getLeftInset() {
  51.       return this.left;
  52.    }
  53.  
  54.    protected final short getRightInset() {
  55.       return this.right;
  56.    }
  57.  
  58.    protected final short getTopInset() {
  59.       return this.top;
  60.    }
  61.  
  62.    public View getView(int n) {
  63.       return this.children[n];
  64.    }
  65.  
  66.    protected abstract View getViewAtPoint(int var1, int var2, Rectangle var3);
  67.  
  68.    protected View getViewAtPosition(int pos, Rectangle a) {
  69.       Element elem = ((View)this).getElement();
  70.       int index = elem.getElementIndex(pos);
  71.       Element child = elem.getElement(index);
  72.       if (child != null && index < this.getViewCount()) {
  73.          View v = this.getView(index);
  74.          if (v.getElement() == child) {
  75.             if (a != null) {
  76.                this.childAllocation(index, a);
  77.             }
  78.  
  79.             return v;
  80.          }
  81.       }
  82.  
  83.       return null;
  84.    }
  85.  
  86.    public int getViewCount() {
  87.       return this.nchildren;
  88.    }
  89.  
  90.    public void insert(int offs, View v) {
  91.       ONE[0] = v;
  92.       this.replace(offs, 0, ONE);
  93.    }
  94.  
  95.    protected abstract boolean isAfter(int var1, int var2, Rectangle var3);
  96.  
  97.    protected abstract boolean isBefore(int var1, int var2, Rectangle var3);
  98.  
  99.    protected void loadChildren(ViewFactory f) {
  100.       Element e = ((View)this).getElement();
  101.       int n = e.getElementCount();
  102.       if (n > 0) {
  103.          View[] added = new View[n];
  104.  
  105.          for(int i = 0; i < n; ++i) {
  106.             added[i] = f.create(e.getElement(i));
  107.          }
  108.  
  109.          this.replace(0, 0, added);
  110.       }
  111.  
  112.    }
  113.  
  114.    public Shape modelToView(int pos, Shape a) throws BadLocationException {
  115.       Rectangle alloc = this.getInsideAllocation(a);
  116.       View v = this.getViewAtPosition(pos, alloc);
  117.       if (v != null) {
  118.          int p0 = v.getStartOffset();
  119.          int p1 = v.getEndOffset();
  120.          if (pos >= p0 && pos < p1) {
  121.             return v.modelToView(pos, alloc);
  122.          }
  123.       }
  124.  
  125.       throw new BadLocationException("Position not represented by view", pos);
  126.    }
  127.  
  128.    public void removeAll() {
  129.       this.replace(0, this.nchildren, ZERO);
  130.    }
  131.  
  132.    public void replace(int offset, int length, View[] views) {
  133.       for(int i = offset; i < offset + length; ++i) {
  134.          this.children[i].setParent((View)null);
  135.       }
  136.  
  137.       int delta = views.length - length;
  138.       int src = offset + length;
  139.       int nmove = this.nchildren - src;
  140.       int dest = src + delta;
  141.       if (this.nchildren + delta >= this.children.length) {
  142.          int newLength = Math.max(2 * this.children.length, this.nchildren + delta);
  143.          View[] newChildren = new View[newLength];
  144.          System.arraycopy(this.children, 0, newChildren, 0, offset);
  145.          System.arraycopy(views, 0, newChildren, offset, views.length);
  146.          System.arraycopy(this.children, src, newChildren, dest, nmove);
  147.          this.children = newChildren;
  148.       } else {
  149.          System.arraycopy(this.children, src, this.children, dest, nmove);
  150.          System.arraycopy(views, 0, this.children, offset, views.length);
  151.       }
  152.  
  153.       this.nchildren += delta;
  154.  
  155.       for(int i = 0; i < views.length; ++i) {
  156.          views[i].setParent(this);
  157.       }
  158.  
  159.    }
  160.  
  161.    protected final void setInsets(short top, short left, short bottom, short right) {
  162.       this.top = top;
  163.       this.left = left;
  164.       this.right = right;
  165.       this.bottom = bottom;
  166.    }
  167.  
  168.    protected final void setParagraphInsets(AttributeSet attr) {
  169.       this.top = (short)((int)StyleConstants.getSpaceAbove(attr));
  170.       this.left = (short)((int)StyleConstants.getLeftIndent(attr));
  171.       this.bottom = (short)((int)StyleConstants.getSpaceBelow(attr));
  172.       this.right = (short)((int)StyleConstants.getRightIndent(attr));
  173.    }
  174.  
  175.    public void setParent(View parent) {
  176.       super.setParent(parent);
  177.       if (parent != null) {
  178.          ViewFactory f = ((View)this).getViewFactory();
  179.          this.loadChildren(f);
  180.       }
  181.  
  182.    }
  183.  
  184.    public int viewToModel(float x, float y, Shape a) {
  185.       Rectangle alloc = this.getInsideAllocation(a);
  186.       if (this.isBefore((int)x, (int)y, alloc)) {
  187.          return ((View)this).getStartOffset();
  188.       } else if (this.isAfter((int)x, (int)y, alloc)) {
  189.          return ((View)this).getEndOffset() - 1;
  190.       } else {
  191.          View v = this.getViewAtPoint((int)x, (int)y, alloc);
  192.          return v != null ? v.viewToModel(x, y, alloc) : -1;
  193.       }
  194.    }
  195. }
  196.